﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Framework.Expand
{
    public class ExAudioSource : MonoBehaviour
    {
        private AudioSource mAudioSource = null;
        private UnityAction mOnPlayOverCallback = null;

        public AudioSource AudioSource
        {
            get { return mAudioSource; }
        }

        public UnityAction OnPlayOverCallback
        {
            set { mOnPlayOverCallback = value; }
        }

        private void Awake()
        {
            mAudioSource = gameObject.AddComponent<AudioSource>();
        }

        public void Play()
        {
            mAudioSource?.Play();
            if (mOnPlayOverCallback != null)
                StartCoroutine(OnPlayOver());
        }

        private IEnumerator OnPlayOver()
        {
            yield return mAudioSource.isPlaying;
            mOnPlayOverCallback?.Invoke();
        }
    }
}